import 'package:color_dart/RgbaColor.dart';
import 'package:flutter/material.dart';
import 'package:im_flutter_sdk_example/jsonserialize/need/data.dart';
import 'package:im_flutter_sdk_example/utils/constants.dart';
import 'package:im_flutter_sdk_example/utils/global.dart';
import 'package:im_flutter_sdk_example/utils/time_util.dart';
import 'package:simple_html_css/simple_html_css.dart';
import 'package:im_flutter_sdk_example/utils/screen.dart';

class NeedItem extends StatelessWidget {
  final bool showFollow;
  final NeedData item;
  const NeedItem({
    Key key,
    this.showFollow = false,
    this.item,
  }) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () => {G.pushNamed('/need_detail', arguments: item)},
      child: Container(
        decoration: BoxDecoration(
            border: Border(
                bottom: BorderSide(
                    width: 1, color: Color.fromRGBO(242, 242, 242, 1)))),
        padding: EdgeInsets.symmetric(vertical: 12),
        child: Column(
          children: [
            if (showFollow)
              Container(
                padding: EdgeInsets.only(top: 6, bottom: 6),
                child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Row(
                        children: [
                          SizedBox(
                            width: 28,
                            height: 28,
                            child: Image.asset(
                              'lib/assets/images/default_avatar.png',
                              fit: BoxFit.cover,
                            ),
                          ),
                          Container(
                              child: Text('云南国科智融科技发展有限公司',
                                  style: TextStyle(
                                      color: rgba(136, 135, 135, 1),
                                      fontSize: 14)),
                              margin: EdgeInsets.only(left: 10)),
                        ],
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Container(
                              height: 22,
                              padding: EdgeInsets.symmetric(horizontal: 6),
                              decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(11),
                                border: Border.all(
                                  color: rgba(25, 109, 236, 1),
                                  width: 1,
                                ),
                              ),
                              child: Text('+ 关注',
                                  style: TextStyle(
                                      color: rgba(25, 109, 236, 1),
                                      fontSize: 12)),
                              margin: EdgeInsets.only(left: 3)),
                        ],
                      ),
                    ]),
              ),
            Row(
              children: [
                Container(
                  width: 300,
                  child: Text(
                    '${item.title}',
                    style: mainTitleStyle,
                    overflow: TextOverflow.ellipsis,
                  ),
                ),
              ],
            ),
            Padding(
              padding: const EdgeInsets.only(top: 10, bottom: 10),
              child: RichText(
                textAlign: TextAlign.start,
                overflow: TextOverflow.ellipsis,
                maxLines: 2,
                text: HTML.toTextSpan(
                  context,
                  item.note,
                ),
                // style: subtitleStyle,
                // maxLines: 2,
              ),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text('需求类型：${item.typeLabel}',
                    style: TextStyle(
                      color: rgba(163, 163, 163, 1),
                      fontSize: 24.nsp,
                    )),
                Text('发布时间：${TimeUtil.stringFromDateTime(item.createDate)}',
                    style: TextStyle(
                      color: rgba(163, 163, 163, 1),
                      fontSize: 24.nsp,
                    )),
                Row(
                  children: [
                    Text('预算：',
                        style: TextStyle(
                          color: rgba(163, 163, 163, 1),
                          fontSize: 24.nsp,
                        )),
                    item.budget == '-1'
                        ? Text('免费',
                            style: TextStyle(
                              color: rgba(235, 101, 13, 1),
                              fontSize: 28.nsp,
                              fontWeight: FontWeight.bold,
                            ))
                        : Text('${item.budget}万元',
                            style: TextStyle(
                              color: rgba(235, 101, 13, 1),
                              fontSize: 28.nsp,
                            ))
                  ],
                )
              ],
            )
          ],
        ),
      ),
    );
  }

  Widget spaceR(Widget child) {
    return Container(
      margin: EdgeInsets.only(right: 10),
      child: child,
    );
  }

  //时间返回年月日
  // String sub_10(DateTime createDate) {
  //   String timestamp =
  //       "${createDate.year.toString()}-${createDate.month.toString().padLeft(2, '0')}-${createDate.day.toString().padLeft(2, '0')}";
  //   return timestamp;
  // }
}

class NeedList extends StatefulWidget {
  final isRecommend;
  NeedList({Key key, this.isRecommend}) : super(key: key);
  @override
  _NeedListState createState() => _NeedListState();
}

class _NeedListState extends State<NeedList>
    with AutomaticKeepAliveClientMixin {
  List<NeedData> listData = [];
  @override
  Widget build(BuildContext context) {
    super.build(context);
    return SingleChildScrollView(
      child: Container(
          color: Colors.white,
          padding: EdgeInsets.symmetric(horizontal: 10),
          child: widget.isRecommend == true && listData.length > 2
              ? Column(
                  children: listData
                      .getRange(0, 3)
                      .map((item) => NeedItem(item: item))
                      .toList(),
                )
              : Column(
                  children:
                      listData.map((item) => NeedItem(item: item)).toList(),
                )),
    );
  }

  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    super.initState();
    this.getData();
  }

  void getData() async {
    G.showLoading(status: '加载中...');
    var res = await G.req.home.demandList();
    res.data['data']['list'].forEach((e) {
      NeedData list = NeedData.fromJson(e);
      listData.add(list);
    });

    this.setState(() {});
    G.dismissLoading();
  }
}
